package org.ermannofranco.genericdaojdbc.user.model;

import java.util.Random;
import java.util.ResourceBundle;

public class ModelUtils {
	public static final String LETTERS = "abcdefghijklmnopqrstuvwxyz";
	public static final int ETA_MAX = 100;
	public static final int ETA_MIN = 5;
	public static final ResourceBundle Nomi_BUNDLE = ResourceBundle
			.getBundle("nomi");
	public static final ResourceBundle Cognomi_BUNDLE = ResourceBundle
			.getBundle("cognomi");
	public static final ResourceBundle Indirizzi_BUNDLE = ResourceBundle
			.getBundle("indirizzi");
	public static final ResourceBundle Citta_BUNDLE = ResourceBundle
			.getBundle("citta");
	public static String[] NOMI;
	public static String[] COGNOMI;
	public static String[] INDIRIZZI;
	public static String[] CITTA;
	public static final Random RND = new Random();
	static {
		int nomiSize = Integer.parseInt(Nomi_BUNDLE.getString("nomi"));
		NOMI = new String[nomiSize];
		for (int i = 0; i < nomiSize; i++) {
			NOMI[i] = Nomi_BUNDLE.getString(String.valueOf(i));
		}

		int cognomiSize = Integer.parseInt(Cognomi_BUNDLE.getString("cognomi"));
		COGNOMI = new String[cognomiSize];
		for (int i = 0; i < cognomiSize; i++) {
			COGNOMI[i] = Cognomi_BUNDLE.getString(String.valueOf(i));
		}
		int indirizziSize = Integer.parseInt(Indirizzi_BUNDLE
				.getString("indirizzi"));
		INDIRIZZI = new String[indirizziSize];
		for (int i = 0; i < indirizziSize; i++) {
			INDIRIZZI[i] = Indirizzi_BUNDLE.getString(String.valueOf(i));
		}
		int cittaSize = Integer.parseInt(Citta_BUNDLE.getString("citta"));
		CITTA = new String[cittaSize];
		for (int i = 0; i < cittaSize; i++) {
			CITTA[i] = Citta_BUNDLE.getString(String.valueOf(i));
		}
	}

	private static String getRandomName() {
		String nome = NOMI[RND.nextInt(NOMI.length)];
		String cognome = COGNOMI[RND.nextInt(COGNOMI.length)];
		return cognome + " " + nome;

	}

	private static String getRandomIndirizzo() {
		return INDIRIZZI[RND.nextInt(INDIRIZZI.length)];
	}

	private static String getRandomCitta() {
		return CITTA[RND.nextInt(CITTA.length)];
	}

	private static String getRandomString(int size) {
		char[] chars = new char[size];
		for (int i = 0; i < chars.length; i++) {
			chars[i] = LETTERS.charAt(RND.nextInt(LETTERS.length()));
		}
		return new String(chars);
	}

	public static User getRandomUser() {
		String rndName = getRandomName();
		// randomizzo il login, per via dell'unique constraint
		String rndLogin = getRandomString(10);
		String rndPassw = rndLogin.substring(0, 5);
		int rndEta = RND.nextInt(ETA_MAX - ETA_MIN) + ETA_MIN;
		int rndTipo = RND.nextInt(TipiUtente.values().length);

		User u = new User(rndEta, rndName, rndLogin, rndPassw, rndTipo, 0);
		u.setEmail(getRandomEmail(rndPassw));
		u.setAddress(getRandomAddress());
		return u;
	}

	private static String getRandomEmail(String name) {
		StringBuilder sb = new StringBuilder(name);
		sb.append("@").append(getRandomString(4)).append(".").append(
				getRandomString(3));
		return sb.toString();
	}

	public static Address getRandomAddress() {
		Address a = new Address();
		a.setAddress(getRandomIndirizzo());
		a.setCity(getRandomCitta());
		return a;
	}

}
